<%@page import="java.text.DecimalFormat"%>
<%@page import="java.text.DecimalFormatSymbols"%>
<%@page import="com.mconnect.msurvey.model.Report"%>
<%@page import="com.mconnect.msurvey.model.SurveyPlan"%>
<%@page import="com.mconnect.msurvey.controller.PlanManager"%>
<%@page import="java.util.List"%>
<%@page import="com.mconnect.msurvey.controller.UserManager"%>
<%@page import="com.mconnect.msurvey.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    <html>
    <head></head>
    <body>
    	<% String id = request.getParameter("id_user");
    		String pageN = request.getParameter("pageNumber");
    		if(id != null){
    			int id_user = Integer.parseInt(id);
    			UserManager uM = new UserManager();
    			User user = uM.getUserById(id_user);
    			PlanManager pM = new PlanManager();
    			
    			int pageNumber = 1;
    			if(pageN != null){
    				pageNumber = Integer.parseInt(pageN);
    			}
    			List<SurveyPlan> planList = pM.getPlanById_user(id_user, pageNumber);
    		%>
    	
    	<%if(pageN ==null){ %>		
    	<div style="margin-top: 30px;">
    	
    		<input type="hidden" id="id_user" value="<%=id_user %>">
	    	<table style="width: 700px; font-size: 15px; font-style: italic;">
	    		<tr>
	    			<td>Imie i nazwisko:</td>
	    			<td><%= user.getFirstname() + " " + user.getLastname() %></td>
	    		</tr>
	    		<tr>
	    			<td>Login:</td>
	    			<td><%= user.getLogin() %></td>
	    		</tr>
	    		<tr>
	    			<td>Ostatnio zalogowany:</td>
	    			<td><%= user.getLoginDate() %></td>
	    		</tr>
	    		<tr>
	    			<td style="height: 30px;">&nbsp</td>
	    			<td></td>
	    		</tr>
	    	</table>
	    	<%} %>
			<script type="text/javascript">
						$(function() {
							var user =  $( "#id_user" ).val();
							
						$('#nextPage1').click(function(){
							var startPage = parseFloat($( "#next1" ).val());
							var page = startPage + 1;
								
							$.get(
								'reports/userSurveyReportContent.jsp',
								{
									pageNumber: page,
									id_user: user
												
								},
								function (content) {
									$('#contents').html(content);
									$( "#next1" ).val(page);
									return false;
								}
							);
								
							});
						
						$('#previousPage1').click(function(){
							var startPage = parseFloat($( "#next1" ).val());
							var page = startPage - 1;
							
							if(page >= 1){
								$.get(
									'reports/userSurveyReportContent.jsp',
									{
										pageNumber: page,
										id_user: user			
									},
									function (content) {
										$('#contents').html(content);
										$( "#next1" ).val(page);
										return false;
									}
								);
							}	
						});
						
					});
				</script>	
	    	<table style="width: 700px; font-size: 15px; " id="contents">
	    	<% for(SurveyPlan plan : planList){ 
	    		Report report = pM.getReportByIdUserAndIdPlan(id_user, plan.getId_plan(), "", "", "all");
	    		double realizationPlan = pM.getRealizationOfPlan(report.getNumberOfConfirms(), report.getNumberOfSurveys());
	    		double cost = pM.getCostForPlan(report.getNumberOfSurveys(), report.getNumberOfRecommend(), realizationPlan, plan.getId_plan());
	    		long days = pM.getNumberOfDaysSpent(plan.getId_plan(), id_user);
	    		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
	            symbols.setDecimalSeparator('.');
	            DecimalFormat df = new DecimalFormat("0.00", symbols);
	    	%>
	    	
	    		<tr style="background-color: #d1d1d1; color:black; font-weight: bold;">
	    			<td>Nazwa kampani:</td>
	    			<td style="float: left;" scope="row"><%=plan.getPlanName() %></td>
	    		</tr>
	    		<tr>
	    			<td>Ilość ankiet z pozyskanymi kontaktami:</td>
	    			<td><%=report.getNumberOfSurveys() %></td>
	    		</tr>
	    		<tr>
	    			<td>Ilość poleceń</td>
	    			<td><%=report.getNumberOfRecommend() %></td>
	    		</tr>
	    		<tr>
	    			<td>Wykaz procentowy realizacji planu:</td>
	    			<td><%=realizationPlan %>% </td>
	    		</tr>
	    		<tr>
	    			<td>Ilość spędzonych dni:</td>
	    			<td><%=days %> </td>
	    		</tr>
	    		<tr>
	    			<td>Ilość ankiet w danym miejscu</td>
	    			<td>
	    				<div style="overflow:auto; max-height: 200px; margin-top: 10px;">
	    					<table>
	    						<%List<String> regionList = pM.getListOfRegionFromSurvey(plan.getId_plan(), id_user);
	    							for(String region : regionList){ 
	    							int numberOfRegion = pM.getNumberOfRegion(plan.getId_plan(), id_user, region);
	    							%>
	    							<tr>
	    								<td width="200px;"> <%=region %></td>
	    								<td> <%= numberOfRegion %></td>
	    							</tr>
	    								
	    						<%}	%>
	    					</table>
	    				</div>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td>Należność za wykonane ankiety</td>
	    			<td style="color: red;"><%= df.format(cost)%> zł</td>
	    		</tr>
	    		<tr>
	    			<td style="height: 30px;">&nbsp</td>
	    			<td></td>
	    		</tr>
	    	
    	<%} %>
    	<tr>
			<td colspan="2" id="form">
				<div>
					<div style="float: left; margin-top: 3px;"><a href="#" id="previousPage1" style="display: block;"><img alt="Previous" src="resources/images/previous.png"> </a></div>
					<div style="float: left; margin-left: 5px; "><input type="text" id="next1" style="width: 20px; text-align: center;"  value="1" ></div>
					<div style="float: left; margin-left: 5px; margin-top: 3px;"><a href="#" id="nextPage1" style="display: block;"><img alt="Next" src="resources/images/next.png"> </a> </div>
				</div>
			</td>
		</tr>
    	</table>
    	</div>
    <% } %> 
    </body>
    </html>